package com.wayne.testapp.activity;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wayne.testapp.R;
import com.wayne.testapp.adapter.HackyViewPager;

import butterknife.ButterKnife;
import butterknife.InjectView;
import uk.co.senab.photoview.PhotoView;

public class TestPhotoActivity extends AppCompatActivity {
    private static final String[] sDrawables = {"http://ww4.sinaimg.cn/mw690/6daf78a1gw1f7ve6rfnx5j23402c0kjl.jpg",
            "http://ww1.sinaimg.cn/mw690/653d411ejw1f7tb6qkg5vj20jg0axjtz.jpg",
            "http://ww3.sinaimg.cn/mw690/6b1394a6gw1f7ve4ke1rgj20m80etwhf.jpg",
            "http://ww2.sinaimg.cn/mw690/0060fv5Fgw1f7ve0oekd5j30kv0rtn19.jpg",
            "http://ww4.sinaimg.cn/mw690/66e8f898gw1f7vdhekgs8j20iw0e6q45.jpg"};
    @InjectView(R.id.tv_notice)
    TextView tv_notice;

    int currPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_photo);
        ButterKnife.inject(this);
        ViewPager mViewPager = (HackyViewPager) findViewById(R.id.view_pager);

        mViewPager.setAdapter(new SamplePagerAdapter(this));
        mViewPager.setCurrentItem(0, true);

        tv_notice.setText(currPage + "/" + sDrawables.length);

        mViewPager.addOnPageChangeListener(new MyPageChangeListener());
    }

    static class SamplePagerAdapter extends PagerAdapter {
        private Context context;

        public SamplePagerAdapter(Context context) {
            this.context = context;
        }


        @Override
        public int getCount() {
            return sDrawables.length;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
//            photoView.setImageResource(sDrawables[position]);
            Glide.with(context).load(sDrawables[position]).into(photoView);
            // Now just add PhotoView to ViewPager and return it
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            return photoView;
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

    public class MyPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            tv_notice.setText((position+1) + "/" + sDrawables.length);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

}
